{
 "metadata": {
  "name": "",
  "signature": "sha256:268380413669e9e6ce1b25e78a0bcc8c0a6286fec6b7814c707bf756a7bd1647"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# Accessing Ensembl with BioServices\n",
      "\n",
      "**This notebook is used to test the BioServices API.** \n",
      "\n",
      "**Test with BioServices 1.3.4**\n",
      "\n",
      "**author: TC**\n",
      "\n",
      "It is based on the Ensembl API and do not provide other examples. \n",
      "For more advanced usage, please see other Ensembl Notebooks provided in BioServices\n",
      "\n",
      "- [Introductory example](#introduction)\n",
      "- [Archive](#archive)\n",
      "- [Comparative genomics](#comparative)\n",
      "- [Cross References](#reference)\n",
      "- [Information](#information)\n",
      "- [Lookup](#lookup)\n",
      "- [Mapping](#mapping)\n",
      "- [Ontology and Taxonomy](#ontology)\n",
      "- [Overlap](#overlap)\n",
      "- [Regulation](#regulation)\n",
      "- [Sequences](#sequences)\n",
      "- [Variation](#variation)\n",
      "\n",
      " **References**   : http://rest.ensembl.org/ \n",
      "             "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bioservices import ensembl\n",
      "# for debugigng\n",
      "reload(ensembl)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 1,
       "text": [
        "<module 'bioservices.ensembl' from '/home/cokelaer/Work/github/bioservices/src/bioservices/ensembl.pyc'>"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e = ensembl.Ensembl()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"introduction\"></a>\n",
      "Introductory example"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "- Most of the methods take one or 2 compulsary arguments\n",
      "- Some are just informative (the get_info family)\n",
      "- An argument that is not part of the Ensembl API is **frmt**. It can be set to one of the Ensembl output format that is. The valid list of format\n",
      "depends on the method. Those two are always available:\n",
      "    - json\n",
      "    - jsonp\n",
      "- By default, output is in json format, which is transformed into a Python dictionary"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_archive('ENSG00000157764')\n",
      "res"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "{u'assembly': u'GRCh38',\n",
        " u'id': u'ENSG00000157764',\n",
        " u'is_current': u'1',\n",
        " u'latest': u'ENSG00000157764.10',\n",
        " u'peptide': None,\n",
        " u'possible_replacement': [],\n",
        " u'release': u'77',\n",
        " u'type': u'Gene',\n",
        " u'version': u'10'}"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In the following example, the format can be phyloxml"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(e.get_archive('ENSG00000157764', frmt='xml'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<html>\n",
        " <head>\n",
        "  <title>\n",
        "   EnsEMBL::REST\n",
        "  </title>\n",
        " </head>\n",
        " <body>\n",
        "  <pre>--- \n",
        "assembly: GRCh38\n",
        "id: ENSG00000157764\n",
        "is_current: 1\n",
        "latest: ENSG00000157764.10\n",
        "peptide: ~\n",
        "possible_replacement: []\n",
        "\n",
        "release: 77\n",
        "type: Gene\n",
        "version: 10\n",
        "</pre>\n",
        " </body>\n",
        "</html>\n"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Here is another example where the requested frmt is json but there is a\n",
      "parameter to specify the format (nh_format)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_genetree_by_member_id('ENSG00000157764', frmt='json', \n",
      "                                  nh_format='phylip')\n",
      "print(res[0:100])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
        "\n",
        "<phyloxml xsi:schemaLocation=\"http://www.phyloxml.org http:/\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "> Here, the input frmt (json) is changed since nh_format can be only in phyloxml format. When  a parameter specifies the format, it may overwrite the value of the argument **frmt** even if provided. \n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# If your identifier is incorrect, you'll get a 500 error code \n",
      "# returned (most probably)\n",
      "wrong = e.get_map_cds_to_region('ENST0000288602', '1..1000')\n",
      "good = e.get_map_cds_to_region('ENST00000288602', '1..1000')\n",
      "wrong, good['mappings'][0]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "(500,\n",
        " {u'assembly_name': u'GRCh38',\n",
        "  u'coord_system': u'chromosome',\n",
        "  u'end': 140924703,\n",
        "  u'gap': 0,\n",
        "  u'rank': 0,\n",
        "  u'seq_region_name': u'7',\n",
        "  u'start': 140924566,\n",
        "  u'strand': -1})"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"archive\"></a>\n",
      "Archive"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Get archived sequence given an identifer\n",
      "archive = e.get_archive('ENSG00000157764')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "identifiers = [\"ENSG00000157764\", \"ENSG00000248378\" ]\n",
      "archives = e.post_archive(identifiers)\n",
      "assert archive == archives[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"comparative\"></a>\n",
      "Comparative genomics"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Gene tree by identifier"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_genetree_by_id('ENSGT00390000003602', nh_format='simple')\n",
      "res['id'], res.keys()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "(u'ENSGT00390000003602', [u'type', u'tree', u'rooted', u'id'])"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_genetree_by_id('ENSGT00390000003602', frmt='phyloxml')\n",
      "print(res[0:200])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
        "\n",
        "<phyloxml xsi:schemaLocation=\"http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"ht\n"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Retrieve genetree by member id and returns a phylip structure\n",
      "This takes a few seconds and output xml is large`"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Here, the input frmt (json) is changed since nh_format can be \n",
      "# only in phyloxml format\n",
      "res = e.get_genetree_by_member_id('ENSG00000157764', frmt='json', \n",
      "                                  nh_format='phylip')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "2235232"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(res[0:500])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
        "\n",
        "<phyloxml xsi:schemaLocation=\"http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.phyloxml.org\">\n",
        "  <phylogeny rooted=\"true\" type=\"gene tree\">\n",
        "    <clade branch_length=\"0\">\n",
        "      <taxonomy>\n",
        "        <id>33213</id>\n",
        "        <scientific_name>Bilateria</scientific_name>\n",
        "      </taxonomy>\n",
        "      <events>\n",
        "        <type>speciation_or_duplication</type>\n",
        "        <duplications>1</dup\n"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_genetree_by_member_symbol('human', 'BRCA2', \n",
      "                                      nh_format='simple')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "print(res[0:200])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "((((((((ENSPFOP00000001575:0.046083,ENSXMAP00000006983:0.065551):0.43822,ENSONIP00000006940:0.359035):0.019582,((ENSTRUP00000015030:0.077336,ENSTNIP00000002435:0.099898):0.208834,ENSGACP00000015199:0.\n"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "region = '2:106040000-106040050'\n",
      "species = 'taeniopygia_guttata'\n",
      "res = e.get_alignment_by_region(region, species, \n",
      "                                species_set_group='sauropsids')\n",
      "res[0]['tree']\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "u'((gallus_gallus_2_100370206_100370256[+]:0.0414,meleagris_gallopavo_3_49885157_49885207[+]:0.0414)Ggal-Mgal[2]:0.1242,taeniopygia_guttata_2_106040000_106040050[+]:0.1715)Ggal-Mgal-Tgut[3]:0.3044;'"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_homology_by_id('ENSG00000157764')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_homology_by_id('ENSG00000157764', frmt='xml')\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_homology_by_id('ENSG00000157764', format='condensed', \n",
      "                           type='orthologues', target_taxon='10090')\n",
      "res['data'][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "{u'homologies': [{u'id': u'ENSMUSG00000002413',\n",
        "   u'method_link_type': u'ENSEMBL_ORTHOLOGUES',\n",
        "   u'protein_id': u'ENSMUSP00000002487',\n",
        "   u'species': u'mus_musculus',\n",
        "   u'taxonomy_level': u'Euarchontoglires',\n",
        "   u'type': u'ortholog_one2one'}],\n",
        " u'id': u'ENSG00000157764'}"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"reference\"></a>\n",
      "Cross references"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_xrefs_by_id('ENST00000288602', external_db='PDB', \n",
      "                        all_levels=True)\n",
      "res[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "{u'db_display_name': u'PDB',\n",
        " u'dbname': u'PDB',\n",
        " u'description': None,\n",
        " u'display_id': u'1UWH',\n",
        " u'info_text': u'',\n",
        " u'info_type': u'DEPENDENT',\n",
        " u'primary_id': u'1UWH',\n",
        " u'synonyms': [],\n",
        " u'version': u'0'}"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_xrefs_by_name('BRCA2', 'human')    \n",
      "res[0]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 21,
       "text": [
        "{u'db_display_name': u'Vega gene',\n",
        " u'dbname': u'Vega_gene',\n",
        " u'description': None,\n",
        " u'display_id': u'BRCA2',\n",
        " u'info_text': u'',\n",
        " u'info_type': u'NONE',\n",
        " u'primary_id': u'OTTHUMG00000017411',\n",
        " u'synonyms': [],\n",
        " u'version': u'1'}"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_xrefs_by_symbol('BRCA2', 'homo_sapiens',\n",
      "                            external_db='HGNC')    \n",
      "res"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 22,
       "text": [
        "[{u'id': u'ENSG00000139618', u'type': u'gene'},\n",
        " {u'id': u'LRG_293', u'type': u'gene'}]"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"information\"></a>\n",
      "Information"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len(e.get_info_analysis('human'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "233"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_assembly('human')['karyotype']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "[u'1',\n",
        " u'2',\n",
        " u'3',\n",
        " u'4',\n",
        " u'5',\n",
        " u'6',\n",
        " u'7',\n",
        " u'8',\n",
        " u'9',\n",
        " u'10',\n",
        " u'11',\n",
        " u'12',\n",
        " u'13',\n",
        " u'14',\n",
        " u'15',\n",
        " u'16',\n",
        " u'17',\n",
        " u'18',\n",
        " u'19',\n",
        " u'20',\n",
        " u'21',\n",
        " u'22',\n",
        " u'X',\n",
        " u'Y',\n",
        " u'MT']"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_assembly_by_region('homo_sapiens', 'X')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 25,
       "text": [
        "{u'assembly_exception_type': u'REF',\n",
        " u'assembly_name': u'GRCh38',\n",
        " u'coordinate_system': u'chromosome',\n",
        " u'is_chromosome': 1,\n",
        " u'length': 156040895}"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "len(e.get_info_biotypes('human'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 26,
       "text": [
        "55"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_compara_methods()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "{u'ConservationScore.conservation_score': [u'GERP_CONSERVATION_SCORE'],\n",
        " u'ConstrainedElement.constrained_element': [u'GERP_CONSTRAINED_ELEMENT'],\n",
        " u'Family.family': [u'FAMILY'],\n",
        " u'GenomicAlignBlock.multiple_alignment': [u'PECAN'],\n",
        " u'GenomicAlignBlock.pairwise_alignment': [u'TRANSLATED_BLAT_NET',\n",
        "  u'BLASTZ_NET',\n",
        "  u'LASTZ_NET',\n",
        "  u'LASTZ_PATCH'],\n",
        " u'GenomicAlignTree.ancestral_alignment': [u'EPO'],\n",
        " u'GenomicAlignTree.tree_alignment': [u'EPO_LOW_COVERAGE'],\n",
        " u'Homology.homology': [u'ENSEMBL_PROJECTIONS',\n",
        "  u'ENSEMBL_PARALOGUES',\n",
        "  u'ENSEMBL_ORTHOLOGUES'],\n",
        " u'NCTree.nc_tree_node': [u'NC_TREES'],\n",
        " u'ProteinTree.protein_tree_node': [u'PROTEIN_TREES'],\n",
        " u'SyntenyRegion.synteny': [u'SYNTENY']}"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_compara_by_method('EPO')[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 28,
       "text": [
        "{u'method': u'EPO',\n",
        " u'name': u'5 teleost fish EPO',\n",
        " u'species_set': [u'oryzias_latipes',\n",
        "  u'takifugu_rubripes',\n",
        "  u'gasterosteus_aculeatus',\n",
        "  u'tetraodon_nigroviridis',\n",
        "  u'danio_rerio'],\n",
        " u'species_set_group': u'fish'}"
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_comparas()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "{u'comparas': [{u'name': u'multi', u'release': 77}]}"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_data()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 30,
       "text": [
        "{u'releases': [77]}"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_info_external_dbs('human')\n",
      "[x['name'] for x in res if 'hgnc' in x['name'].lower()]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 31,
       "text": [
        "[u'HGNC',\n",
        " u'HGNC_curated_gene',\n",
        " u'HGNC_automatic_gene',\n",
        " u'HGNC_curated_transcript',\n",
        " u'HGNC_automatic_transcript',\n",
        " u'HGNC_trans_name']"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_ping()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "1"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_info_rest()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 33,
       "text": [
        "{u'release': u'3.1.0'}"
       ]
      }
     ],
     "prompt_number": 33
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_info_software()\n",
      "res"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 34,
       "text": [
        "{u'release': 77}"
       ]
      }
     ],
     "prompt_number": 34
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_info_species()\n",
      "[x['name'] for x in res['species'] if 'ovis' in x['name']]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 35,
       "text": [
        "[u'ovis_aries']"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"lookup\"></a>\n",
      "Lookup"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_lookup_by_id('ENSG00000157764', expand=True)\n",
      "res.keys()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 36,
       "text": [
        "[u'assembly_name',\n",
        " u'display_name',\n",
        " u'description',\n",
        " u'seq_region_name',\n",
        " u'logic_name',\n",
        " u'object_type',\n",
        " u'start',\n",
        " u'id',\n",
        " u'source',\n",
        " u'db_type',\n",
        " u'biotype',\n",
        " u'end',\n",
        " u'Transcript',\n",
        " u'species',\n",
        " u'strand']"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.post_lookup_by_id([\"ENSG00000157764\", \"ENSG00000248378\" ], \n",
      "                          expand=0)\n",
      "res['ENSG00000157764']\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 37,
       "text": [
        "{u'assembly_name': u'GRCh38',\n",
        " u'biotype': u'protein_coding',\n",
        " u'db_type': u'core',\n",
        " u'description': u'B-Raf proto-oncogene, serine/threonine kinase [Source:HGNC Symbol;Acc:HGNC:1097]',\n",
        " u'display_name': u'BRAF',\n",
        " u'end': 140924764,\n",
        " u'id': u'ENSG00000157764',\n",
        " u'logic_name': u'ensembl_havana_gene',\n",
        " u'object_type': u'Gene',\n",
        " u'seq_region_name': u'7',\n",
        " u'source': u'ensembl_havana',\n",
        " u'species': u'homo_sapiens',\n",
        " u'start': 140719327,\n",
        " u'strand': -1}"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_lookup_by_symbol('homo_sapiens', 'BRCA2', expand=True)\n",
      "len(res['Transcript'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 38,
       "text": [
        "7"
       ]
      }
     ],
     "prompt_number": 38
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.post_lookup_by_symbol('human', [\"BRCA2\", \"BRAF\" ], expand=True)\n",
      "len(res['BRCA2']['Transcript'])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 39,
       "text": [
        "7"
       ]
      }
     ],
     "prompt_number": 39
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"mapping\"></a>\n",
      "Mapping"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\tDescription\n",
      "- Convert from cDNA coordinates to genomic coordinates. Output reflects forward orientation coordinates as returned from the Ensembl API.\n",
      "- GET map/cds/:id/:region \tConvert from CDS coordinates to genomic coordinates. Output reflects forward orientation coordinates as returned from the Ensembl API.\n",
      "- GET map/:species/:asm_one/:region/:asm_two \tConvert the co-ordinates of one assembly to another\n",
      "- GET map/translation/:id/:region \tConvert from protein (translation) coordinates to genomic coordinates. Output reflects forward orientation coordinates as returned from the Ensembl "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# the commented statement does not work\n",
      "# res = e.get_map_assembly_one_to_two('GRCh37', 'NCBI36',\n",
      "# region='X:10000000..1000100:1', species='human')\n",
      "res = e.get_map_assembly_one_to_two('GRCh37', 'GRCh38', \n",
      "                                    region='X:1000000..1000100:1')\n",
      "res"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 40,
       "text": [
        "{u'mappings': [{u'mapped': {u'assembly': u'GRCh38',\n",
        "    u'coordinate_system': u'chromosome',\n",
        "    u'end': 1039365,\n",
        "    u'seq_region_name': u'X',\n",
        "    u'start': 1039265,\n",
        "    u'strand': 1},\n",
        "   u'original': {u'assembly': u'GRCh37',\n",
        "    u'coordinate_system': u'chromosome',\n",
        "    u'end': 1000100,\n",
        "    u'seq_region_name': u'X',\n",
        "    u'start': 1000000,\n",
        "    u'strand': 1}}]}"
       ]
      }
     ],
     "prompt_number": 40
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_map_translation_to_region('ENSP00000288602', '100..300')\n",
      "res['mappings'][0]  "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 41,
       "text": [
        "{u'assembly_name': u'GRCh38',\n",
        " u'coord_system': u'chromosome',\n",
        " u'end': 140834815,\n",
        " u'gap': 0,\n",
        " u'rank': 0,\n",
        " u'seq_region_name': u'7',\n",
        " u'start': 140834609,\n",
        " u'strand': -1}"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_map_cds_to_region('ENST00000288602', '1..1000')\n",
      "res['mappings'][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 42,
       "text": [
        "{u'assembly_name': u'GRCh38',\n",
        " u'coord_system': u'chromosome',\n",
        " u'end': 140924703,\n",
        " u'gap': 0,\n",
        " u'rank': 0,\n",
        " u'seq_region_name': u'7',\n",
        " u'start': 140924566,\n",
        " u'strand': -1}"
       ]
      }
     ],
     "prompt_number": 42
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_map_cdna_to_region('ENST00000288602', '100..300')\n",
      "res['mappings'][0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 43,
       "text": [
        "{u'assembly_name': u'GRCh38',\n",
        " u'coord_system': u'chromosome',\n",
        " u'end': 140924665,\n",
        " u'gap': 0,\n",
        " u'rank': 0,\n",
        " u'seq_region_name': u'7',\n",
        " u'start': 140924566,\n",
        " u'strand': -1}"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"ontology\"></a>\n",
      "Ontologies and Taxonomy"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_ontology_ancestors_by_id('GO:0005667')\n",
      "res[0].keys()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 44,
       "text": [
        "[u'definition',\n",
        " u'name',\n",
        " u'subsets',\n",
        " u'namespace',\n",
        " u'accession',\n",
        " u'synonyms',\n",
        " u'ontology']"
       ]
      }
     ],
     "prompt_number": 44
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_ontology_ancestors_chart_by_id('GO:0005667')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_ontology_descendants_by_id('GO:0005667')\n",
      "res[0]['accession']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 46,
       "text": [
        "u'GO:0043234'"
       ]
      }
     ],
     "prompt_number": 46
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_ontology_by_id('GO:0005667')\n",
      "res['accession']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 47,
       "text": [
        "u'GO:0005667'"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_ontology_by_name('transcription factor complex')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 48
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_taxonomy_classification_by_id(9606)\n",
      "res[0]['children']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 49,
       "text": [
        "[{u'id': u'9606',\n",
        "  u'leaf': 0,\n",
        "  u'name': u'Homo sapiens',\n",
        "  u'scientific_name': u'Homo sapiens',\n",
        "  u'tags': {u'authority': [u'Homo sapiens Linnaeus, 1758'],\n",
        "   u'common name': [u'man'],\n",
        "   u'ensembl alias name': [u'Human'],\n",
        "   u'genbank common name': [u'human'],\n",
        "   u'name': [u'Homo sapiens'],\n",
        "   u'scientific name': [u'Homo sapiens']}}]"
       ]
      }
     ],
     "prompt_number": 49
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_taxonomy_by_name('Homo')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 50
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_taxonomy_by_id(9606)['scientific_name']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 51,
       "text": [
        "u'Homo sapiens'"
       ]
      }
     ],
     "prompt_number": 51
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"overlap\"></a>\n",
      "Overlap"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_overlap_by_id(\"ENSG00000157764\", feature='gene')\n",
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 52,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 52
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#ture=transcript;feature=cds;feature=exon\n",
      "res = e.get_overlap_by_region('7:140424943-140624564', \n",
      "                              species='human', feature='gene')\n",
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 53,
       "text": [
        "4"
       ]
      }
     ],
     "prompt_number": 53
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_overlap_by_translation('ENSP00000288602', type='Superfamily')\n",
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 54,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 54
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#feature=transcript_variation;contson;\n",
      "res = e.get_overlap_by_translation('ENSP00000288602', \n",
      "                                   type='missense_variant',\n",
      "                                   feature='transcript_variation')\n",
      "\n",
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 55,
       "text": [
        "113"
       ]
      }
     ],
     "prompt_number": 55
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_overlap_by_translation('ENSP00000288602', \n",
      "                                   type='missense_variant',\n",
      "                                   feature='somatic_transcript_variation')\n",
      "\n",
      "len(res)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 56,
       "text": [
        "215"
       ]
      }
     ],
     "prompt_number": 56
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"regulation\"></a>\n",
      "Regulation"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_regulatory_by_id('ENSR00001348195', 'human')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 57,
       "text": [
        "{u'ID': u'ENSR00001348195',\n",
        " u'activity_evidence': 1,\n",
        " u'bound_end': 48551079,\n",
        " u'bound_start': 48538280,\n",
        " u'cell_type': u'MultiCell',\n",
        " u'description': u'Predicted promoter',\n",
        " u'end': 48545479,\n",
        " u'feature_type': u'regulatory',\n",
        " u'seq_region_name': u'17',\n",
        " u'start': 48541080,\n",
        " u'strand': u'0'}"
       ]
      }
     ],
     "prompt_number": 57
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=\"sequences\"></a>\n",
      "Sequences"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_id('ENSG00000157764', frmt='text')\n",
      "print(sequence[0:120])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "CGCCTCCCTTCCCCCTCCCCGCCCGACAGCGGCCGCTCGGGCCCCGGCTCTCGGTTATAAGATGGCGGCGCTGAGCGGTGGCGGTGGTGGCGGCGCGGAGCCGGGCCAGGCTCTGTTCAA\n"
       ]
      }
     ],
     "prompt_number": 58
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_id('ENSG00000157764', frmt='fasta')\n",
      "print(sequence[0:120])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        ">ENSG00000157764 chromosome:GRCh38:7:140719327:140924764:-1\n",
        "CGCCTCCCTTCCCCCTCCCCGCCCGACAGCGGCCGCTCGGGCCCCGGCTCTCGGTTATAA\n"
       ]
      }
     ],
     "prompt_number": 59
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_id('CCDS5863.1', frmt='fasta', \n",
      "                     object_type='transcript', db_type='otherfeatures',\n",
      "                     type='cds', species='human')\n",
      "print(sequence[0:120])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        ">CCDS5863.1\n",
        "ATGGCGGCGCTGAGCGGTGGCGGTGGTGGCGGCGCGGAGCCGGGCCAGGCTCTGTTCAAC\n",
        "GGGGACATGGAGCCCGAGGCCGGCGCCGGCGCCGGCGCCGCGGCCTC\n"
       ]
      }
     ],
     "prompt_number": 60
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_id('ENSG00000157764', frmt='seqxml',\n",
      "                                multiple_sequences=True,type='protein')\n",
      "print(sequence[0:240])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
        "\n",
        "<seqXML xsi:noNamespaceSchemaLocation=\"http://www.seqxml.org/0.4/seqxml.xsd\" seqXMLversion=\"0.4\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n",
        "  <entry id=\"ENSP00000419060\">\n",
        "    <AAseq>XSTTGL\n"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_region('X:1000000..1000100:1', 'human')\n",
      "sequence"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 62,
       "text": [
        "{u'id': u'chromosome:GRCh38:X:1000000:1000100:1',\n",
        " u'molecule': u'dna',\n",
        " u'seq': u'ctgtagaaacattagcctggctaacaaggtgaaaccccatctctactaacaatacaaaatattggttgggcgtggtggcgggtgcttgtaatcccagctac'}"
       ]
      }
     ],
     "prompt_number": 62
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = e.get_sequence_by_region('ABBA01004489.1:1..100', 'human',\n",
      "                                    frmt='json', coord_system='seqlevel')\n",
      "sequence"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 63,
       "text": [
        "{u'id': u'contig::ABBA01004489.1:1:100:1',\n",
        " u'molecule': u'dna',\n",
        " u'seq': u'ctgtactttccttgggatggagtagtttcgaaacacactttctgtagaatctgcaagtggatatttggacctgtctgaggaattcgttggaaacgggata'}"
       ]
      }
     ],
     "prompt_number": 63
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "<a name=variation></a>\n",
      "Variation"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "e.get_variation_by_id('rs56116432', 'human')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 64,
       "text": [
        "{u'MAF': u'0.00367309',\n",
        " u'ambiguity': u'Y',\n",
        " u'ancestral_allele': u'C',\n",
        " u'evidence': [u'Multiple_observations', u'1000Genomes', u'ESP'],\n",
        " u'mappings': [{u'allele_string': u'C/T',\n",
        "   u'assembly_name': u'GRCh38',\n",
        "   u'coord_system': u'chromosome',\n",
        "   u'end': 133256042,\n",
        "   u'location': u'9:133256042-133256042',\n",
        "   u'seq_region_name': u'9',\n",
        "   u'start': 133256042,\n",
        "   u'strand': 1}],\n",
        " u'most_severe_consequence': u'Missense variant',\n",
        " u'name': u'rs56116432',\n",
        " u'source': u'Variants (including SNPs and indels) imported from dbSNP (mapped to GRCh38)',\n",
        " u'synonyms': [],\n",
        " u'var_class': u'SNP'}"
       ]
      }
     ],
     "prompt_number": 64
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_vep_by_id('COSM476', 'human')\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 65
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res = e.get_vep_by_id('rs116035550', 'human')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 66
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "res =e.get_vep_by_region('9:22125503-22125502:1', 'C', 'human')\n",
      "res[0]['most_severe_consequence']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 67,
       "text": [
        "u'downstream_gene_variant'"
       ]
      }
     ],
     "prompt_number": 67
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}